import os
import pandas as pd
from pprint import pprint


class DataOperation:
    def __init__(self, path):
        try:
            if path.endswith('.csv') or path.endswith('.txt'):
                self.data = pd.read_csv(path)
            elif path.endswith('.xlsx') or path.endswith('.xls'):
                self.data = pd.read_excel(path)
            else:
                print('不支持该文件类型')
        except (IndentationError, AttributeError, FileNotFoundError):
            print('路径不正确/文件名错误/文件格式错误')

    def get_data_to_dict(self):
        try:
            return [self.data.loc[i].to_dict() for i in self.data.index.values]
        except (IndentationError, AttributeError, FileNotFoundError):
            print('路径不正确/文件名错误/文件格式错误')

    def get_data_to_list(self):
        try:
            return self.data.values.tolist()
        except (IndentationError, AttributeError, FileNotFoundError):
            print('路径不正确/文件名错误/文件格式错误')

if __name__ == '__main__':
    project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    path = os.path.join(project_path, 'data/lj_data.csv')   # join 专门用来路径拼接
    data = DataOperation(path)
    pprint(data.get_data_to_list())
    pprint(data.get_data_to_dict())